home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / textool / pxlstuff.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  5KB  |  174 lines

  1. /*
  2.  * This is the font specific bit of textool.
  3.  * This version supports PXL format files, which are easy to use but big.
  4.  */
  5.  
  6. #include "defs.h"
  7. #include <stdio.h>
  8. #include <sys/file.h>                /* for access() stuff */
  9. #include <pixrect/pixrect_hs.h>        /* for LoadAChar() */
  10. char *index();
  11. char *rindex();
  12. char *sprintf();
  13. char *strcpy();
  14.  
  15. /* procedures in sunstuff */
  16. extern void    Fatal();
  17. extern void    Warning();
  18.  
  19. /* procedures in dvistuff */
  20. extern float    ActualFactor();
  21. extern int    NoSignExtend();
  22. extern FILE    *OpenFontFile();
  23.  
  24. /* procedures here */
  25. void    FindFontFile();
  26. void    LoadAChar();
  27. void    ReadCharDefs();
  28.  
  29. /* a few global variables and data structures */
  30. #include "globals.h"
  31.  
  32. /* now to the real code */
  33.  
  34. /*-->FindFontFile*/
  35. /**********************************************************************/
  36. /************************  FindFontFile  ******************************/
  37. /**********************************************************************/
  38.  
  39. void
  40. FindFontFile(tfontptr, mag)
  41. struct font_entry *tfontptr;
  42. unsigned int mag;
  43. {
  44.     char *direct, *tcp, *tcp1;
  45.     int found;
  46.     char curarea[STRSIZE];
  47.  
  48.     tfontptr->font_mag = (int)((ActualFactor((int)(((float)tfontptr->s/
  49.                 (float)tfontptr->d)*1000.0 + 0.5)) * 
  50. #ifdef USEGLOBALMAG
  51.                 ActualFactor(mag) *
  52. #endif
  53.                 (float)RESOLUTION * 5.0) + 0.5);
  54.     if (tfontptr->a != 0) {
  55.         sprintf(tfontptr->name, "%s.%dpxl", tfontptr->n, tfontptr->font_mag);
  56.     }
  57.     else {
  58.         direct = PXLpath;
  59.         found = FALSE;
  60.         do { 
  61.             tcp = index(direct, ':');
  62.             if (tcp == NULL)
  63.                 tcp = strlen(direct) + direct;
  64.             strncpy(curarea, direct, tcp-direct);
  65.             tcp1 = curarea + (tcp - direct);
  66.             *tcp1++ = '/';
  67.             *tcp1++ = '\0';
  68.  
  69.             sprintf(tfontptr->name, "%s%s.%dpxl",
  70.                 curarea, tfontptr->n, tfontptr->font_mag);
  71.             found = (access(tfontptr->name, R_OK) == 0);
  72.             if (*tcp)
  73.                 direct = tcp + 1;
  74.             else
  75.                 direct = tcp;
  76.         } while ( !found && *direct != '\0');
  77.     }
  78. }
  79.  
  80.  
  81. /*-->LoadAChar*/
  82. /**********************************************************************/
  83. /*****************************  LoadAChar  ****************************/
  84. /**********************************************************************/
  85.  
  86. void
  87. LoadAChar(ptr)
  88. register struct char_entry *ptr;
  89. {
  90.     register Pixrect *pr;
  91.     register int nshorts, i, col, nints;
  92.     register short *dp, *sp;
  93.     int buffer[8];
  94.     FILE *pxlfp;
  95.  
  96.     pxlfp = OpenFontFile();
  97.     if (ptr->where.address.fileOffset == 0) {
  98.         ptr->where.address.pixrectptr = NULL;
  99.         return;
  100.     }
  101.     fseek(pxlfp, ptr->where.address.fileOffset, 0);
  102.     if ((pr = mem_create(ptr->width, ptr->height, 1)) == NULL) {
  103.         Fatal("Couldn't allocate pixrect");
  104.         return;
  105.     }
  106.     nshorts = (ptr->width + 15) >> 4;
  107.                 /*
  108.                  * the following test is done because
  109.                  * some bogus font files once caused
  110.                  * the pixrect to be overfilled, damaging
  111.                  * other font data structures
  112.                  */
  113.     if (nshorts != (((struct mpr_data *)pr->pr_data)->md_linebytes >> 1)) {
  114.         Fatal("Pixrect too narrow for font");
  115.         return;
  116.     }
  117.     nints = (nshorts + 1) >> 1;
  118.     dp = ((struct mpr_data *)pr->pr_data)->md_image;
  119.     for (col = 0; col < ptr->height; col++) {
  120.         fread(buffer, 4, nints, pxlfp);
  121.         sp = (short *)&buffer[0];
  122.         for (i = nshorts; i > 0; i--)
  123.             *dp++ = *sp++;
  124.     }
  125.     ptr->where.address.pixrectptr = pr;
  126.     ptr->where.isloaded = TRUE;
  127. }
  128.  
  129.  
  130. /*-->ReadCharDefs*/
  131. /**********************************************************************/
  132. /*****************************  ReadCharDefs  *************************/
  133. /**********************************************************************/
  134.  
  135. void
  136. ReadCharDefs(tfontptr, pxlfp)
  137. struct font_entry *tfontptr;
  138. FILE *pxlfp;
  139. {
  140.     int t, i;
  141.     register struct char_entry *tcharptr; /* temporary char_entry pointer */
  142.  
  143.     if ((t = NoSignExtend(pxlfp, 4)) != PXLID) {
  144.         Fatal("PXL ID = %d, can only process PXL ID = %d files",
  145.                 t, PXLID);
  146.         return;
  147.     }
  148.     fseek(pxlfp, -20, 2);
  149.     t = NoSignExtend(pxlfp, 4);
  150.     if ((tfontptr->c != 0) && (t != 0) && (tfontptr->c != t))
  151.         Warning("font = \"%s\",\n-->font checksum = %d,\n-->dvi checksum = %d",
  152.                 tfontptr->name, tfontptr->c, t);
  153.     (void)NoSignExtend(pxlfp, 4);    /* throw away magnification */
  154.     (void)NoSignExtend(pxlfp, 4);    /* throw away design size */
  155.  
  156.     fseek(pxlfp, NoSignExtend(pxlfp, 4) * 4, 0);
  157.  
  158.     for (i = FIRSTPXLCHAR; i <= LASTPXLCHAR; i++) {
  159.         tcharptr = &(tfontptr->ch[i]);
  160.         tcharptr->width = NoSignExtend(pxlfp, 2);
  161.         tcharptr->height = NoSignExtend(pxlfp, 2);
  162.         tcharptr->xOffset= SignExtend(pxlfp, 2);
  163.         tcharptr->yOffset = SignExtend(pxlfp, 2);
  164.         tcharptr->where.isloaded = FALSE;
  165.         tcharptr->where.address.fileOffset = NoSignExtend(pxlfp, 4) << 2;
  166.         tcharptr->tfmw = ((float)NoSignExtend(pxlfp, 4)*(float)tfontptr->s) /
  167.             (float)(1<<20);
  168.     }
  169.     if (BigPreLoad)    /* load all char pixrects */
  170.     for (i = FIRSTPXLCHAR; i <= LASTPXLCHAR; i++)
  171.         LoadAChar(&(tfontptr->ch[i]));
  172. }
  173.  
  174.